<template>
  <el-dialog
    :model-value="props.tkFormVisible"
    :title="title"
    width="30%"
    :before-close="handleClose"
  >
    <div class="scheme-form">
      <el-form
        ref="ruleFormRef"
        :rules="rules"
        :model="form"
        label-width="120px"
      >
        <el-form-item label="名称" prop="questionName">
          <el-input v-model="form.questionName" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">返回</el-button>
        <el-button type="primary" @click="handle(ruleFormRef)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue";
import { useAppStore, paramsDataStore } from "@/stores";
import {
  questionBankSave,
  questionBankUpdate,
  miswiringBankSave,
  miswiringBankUpdate,
} from "@/api/source.js";
import { ElMessage } from "element-plus";
import { storeToRefs } from "pinia";

const useApp = useAppStore();
let { organStructure, currentPlanId } = storeToRefs(useApp);

const emit = defineEmits(["is-show"]);
const props = defineProps({
  tkFormVisible: Boolean,
  form: Object,
  title: String,
  tkType: Number,
});
const rules = reactive({
  questionName: [
    { required: true, message: "请输入题库名称", trigger: "blur" },
  ],
});
const ruleFormRef = ref();

const handleClose = () => {
  emit("is-show");
};
//点击确定
const handle = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let parm = {
        questionName: props.form.questionName,
      };
      if (props.tkType === 1) {
        doTkWiring(parm);
      } else {
        doTk(parm);
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
//试题题库
const doTk = async (parm) => {
  if (props.form.id) {
    parm.id = props.form.id;
    await questionBankUpdate(parm)
      .then((res) => {
        ElMessage({
          message: "保存成功",
          type: "success",
        });
        emit("is-show");
      })
      .catch(() => {
        ElMessage({
          message: "保存失败",
          type: "warning",
        });
      });
  } else {
    await questionBankSave(parm)
      .then((res) => {
        ElMessage({
          message: "保存成功",
          type: "success",
        });
        emit("is-show");
      })
      .catch(() => {
        ElMessage({
          message: "保存失败",
          type: "warning",
        });
      });
  }
};
//错接线题库
const doTkWiring = async (parm) => {
  if (props.form.id) {
    parm.id = props.form.id;
    await miswiringBankUpdate(parm)
      .then((res) => {
        ElMessage({
          message: "保存成功",
          type: "success",
        });
        emit("is-show");
      })
      .catch(() => {
        ElMessage({
          message: "保存失败",
          type: "warning",
        });
      });
  } else {
    await miswiringBankSave(parm)
      .then((res) => {
        ElMessage({
          message: "保存成功",
          type: "success",
        });
        emit("is-show");
      })
      .catch(() => {
        ElMessage({
          message: "保存失败",
          type: "warning",
        });
      });
  }
};
</script>
<style lang="scss" scoped>
.ele-type {
  display: flex;
  .ele-type-item1 {
    width: 390px;
  }
  .ele-type-item2 {
    flex: 1;
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
  .el-button {
    width: 60px;
  }
}
</style>
